package cc.orange.mainView;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import cc.orange.base.BaseFragment;
import cc.orange.entity.FriendItemInfo;
import cc.orange.http.ApiService;

import cc.orange.databinding.FragmentChatFriendItemBinding;

import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.tendcloud.tenddata.TalkingDataSDK;
import com.xcheng.retrofit.Call;
import com.xcheng.retrofit.DefaultCallback;
import com.xcheng.retrofit.HttpError;
import com.xcheng.retrofit.RetrofitFactory;

import java.util.ArrayList;
import java.util.List;

import cc.orange.R;
import cc.orange.adapter.ChatFriendItemAdapter;
import cc.orange.entity.BaseEntity;
import cc.orange.utils.ZXToastUtil;

public class ChatFriendItem2Fragment extends BaseFragment implements View.OnClickListener {
    private FragmentChatFriendItemBinding binding;

    private ChatFriendItemAdapter topChatFriendItemAdapter, bottomChatFriendItemAdapter;

    private List<FriendItemInfo.DataDTO.SocWomenDTO> topItem = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_chat_friend_item, container, false);
        init();
        getFriendList();
        return binding.getRoot();
    }

    private void getFriendList() {
        RetrofitFactory.create(ApiService.class)
                .getFansList(getTokens())
                .enqueue(new DefaultCallback<FriendItemInfo>() {
                    @Override
                    public void onStart(Call<FriendItemInfo> call) {
                        showLoading_base(getActivity());
                    }

                    @Override
                    public void onError(Call<FriendItemInfo> call, HttpError error) {
                        cancelLoading();
                        binding.viewEmpty.setVisibility(View.VISIBLE);
                        binding.includeEmpty.tvEmpty.setText("暂无粉丝");
                        binding.rvTopItem.setVisibility(View.GONE);
                    }

                    @Override
                    public void onSuccess(Call<FriendItemInfo> call, FriendItemInfo friendItemInfo) {
                        cancelLoading();
                        if (binding.smartRL.getState() == RefreshState.Refreshing) {
                            binding.smartRL.finishRefresh();
                        }
                        if (friendItemInfo.getCode() == 0) {
                            FriendItemInfo.DataDTO data = friendItemInfo.getData();
                            List<FriendItemInfo.DataDTO.SocWomenDTO> socSuser = data.getSocSuser();
                            List<FriendItemInfo.DataDTO.SocWomenDTO> socWomen = data.getSocWomen();
                            topItem.clear();
                            if (socSuser != null && socSuser.size() > 0) {
                                topItem.addAll(socSuser);
                            }
                            if (socWomen != null && socWomen.size() > 0) {
                                topItem.addAll(socWomen);
                            }
                            if (topItem.size() == 0) {
                                binding.viewEmpty.setVisibility(View.VISIBLE);
                                binding.includeEmpty.tvEmpty.setText("暂无粉丝");
                                binding.rvTopItem.setVisibility(View.GONE);
                            } else {
                                binding.viewEmpty.setVisibility(View.GONE);
                                binding.rvTopItem.setVisibility(View.VISIBLE);
                                topChatFriendItemAdapter.updateData(topItem);
                            }
                        } else {
                            binding.viewEmpty.setVisibility(View.GONE);
                        }
                    }
                });
    }

    private void init() {
        binding.llRecommend.setVisibility(View.GONE);
        binding.rvBottomItem.setVisibility(View.GONE);
        topChatFriendItemAdapter = new ChatFriendItemAdapter(topItem, 3);
        binding.rvTopItem.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.rvTopItem.setAdapter(topChatFriendItemAdapter);
        topChatFriendItemAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent(getActivity(), ConversationListActivity.class);
            intent.putExtra("uid", topItem.get(position).getNumber());
            intent.putExtra("name",topItem.get(position).getName());
            startActivity(intent);
        });

        binding.ivChange.setOnClickListener(this);
        binding.tvChange.setOnClickListener(this);

        topChatFriendItemAdapter.setOnFollowClick((pos, view) -> {
            FriendItemInfo.DataDTO.SocWomenDTO socWomenDTO = topItem.get(pos);
            attention(0, socWomenDTO.getNumber(), view);
        });

    }

    private void attention(int flag, String uid, View view){
        // 0 确认, 1 否定
        RetrofitFactory.create(ApiService.class)
                .attention(getTokens(),uid,""+flag)
                .enqueue(new DefaultCallback<BaseEntity>() {
                    @Override
                    public void onStart(Call<BaseEntity> call) {
                        showLoading_base(getActivity());
                    }

                    @Override
                    public void onError(Call<BaseEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<BaseEntity> call, BaseEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            TextView tv = (TextView) view;
                            tv.setText("已关注");
                            tv.setTextColor(Color.parseColor("#aaaaaa"));
                            tv.setBackgroundResource(R.drawable.bg_chat_unfollow);
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }


    @Override
    public void onClick(View view) {
        Log.e("ste", "change: " );
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    @Override
    public void onResume() {
        super.onResume();
        TalkingDataSDK.onPageBegin(getActivity().getApplicationContext(), this.toString());
    }

    @Override
    public void onPause() {
        if(!this.isHidden()){
            TalkingDataSDK.onPageEnd(getActivity().getApplicationContext(), this.toString());
        }
        super.onPause();
    }
}
